package com.example.administrator.szb.view;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.bean.IndexBean;
import com.example.administrator.szb.bean.SuPeiBuiness;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.QTLog;
import com.kerchin.widget.GridItemClickListener;
import com.kerchin.widget.GridViewPager;
import com.kerchin.widget.Model;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by stone on 2018/1/22.
 */

public class HomeIconPop {
    private static PopupWindow popupWindow;
    private static Activity context;

    private static GridViewPager mGridViewPager;
    public static void show(Activity ctx, List<SuPeiBuiness> suPeiBuiness, View view, OnItemClickLintener lintener){
        context = ctx;
            QTLog.e("popWindow is null");
            final View popView = LayoutInflater.from(context).inflate(
                    R.layout.homeiconpop, null);
            TextView title = popView.findViewById(R.id.title);
            TextView tips = popView.findViewById(R.id.tips);
            //title.setText(suPeiBuiness.getTitle());
            //tips.setText(suPeiBuiness.getDeclare());
            LinearLayout iv_close = popView.findViewById(R.id.iv_close);
            iv_close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (popupWindow!=null)
                    popupWindow.dismiss();
                }
            });
            mGridViewPager = popView.findViewById(R.id.mGridViewPager);
            initmGridViewPager(lintener,suPeiBuiness);

            popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            setBackgroundAlpha(0.5f);
            ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.white));
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
            popupWindow.setClippingEnabled(false);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    popupWindow=null;
                    setBackgroundAlpha(1.0f);
                }
            });

        if (!popupWindow.isShowing())
        popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
    }
    public static void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = (context).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) context).getWindow().setAttributes(lp);
    }
    private static void initmGridViewPager(final OnItemClickLintener lintener,final List<SuPeiBuiness> suPeiBuiness) {

        mGridViewPager
                //设置每一页的容量
                .setPageSize(suPeiBuiness.size())
                .setCulm(4)
                .setShowAmin(true)
                .setGridItemClickListener(new GridItemClickListener() {
                    @Override
                    public void click(int pos, int position, String str) {
                        popupWindow.dismiss();
                        IndexBean indexBean = SampleApplicationLike.getIndexBeanInstance();
                        if (lintener!=null){
                            int id = 0;
                            if (suPeiBuiness.get(position).getMold()==4){
                                id = 0;
                            }else {
                                id = suPeiBuiness.get(position).getId();
                            }

                            lintener.onItemClick(str,id,position);
                        }
                    }
                })

                //传入String的List 必须作为最后一步
                .init(initDatas(suPeiBuiness));
    }
    /**
     * 作为测试的数据初始化
     *
     * @return
     */
    private static List<Model> initDatas(List<SuPeiBuiness> suPeiBuiness) {
        
        if (suPeiBuiness==null){
            suPeiBuiness = new ArrayList<>();
        }
        List mData = new ArrayList();
        try{
            for (int i = 0; i < suPeiBuiness.size(); i++) {
                String imageId = URLAddress.HOST_PORT+suPeiBuiness.get(i).getCicon();
                String name = suPeiBuiness.get(i).getTitle();
                mData.add(new Model(name, imageId));
            }
            //mData.add(new Model("路演调研", null,R.drawable.luyan_icon));
            return mData;
        }catch (Exception e){

        }
        mData.add(new Model("路演调研", null,R.drawable.luyan_icon));
        QTLog.e(""+mData.size());
        return mData;
    }

    public interface OnItemClickLintener{
        void onItemClick(String name, int id, int pos);
    }
}
